<template>
  <div class="manager-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="8">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          添加
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="16">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.keyword"
              placeholder="请输入姓名/手机号"
              clearable
            />
          </el-form-item>
          <!-- <el-form-item>
            <el-select
              v-model="queryForm.status"
              placeholder="请选择状态"
              clearable
            >
              <el-option label="启用" value="启用" />
              <el-option label="禁用" value="禁用" />
            </el-select>
          </el-form-item> -->
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column
        align="center"
        show-overflow-tooltip
        label="序号"
        type="index"
        width="95"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="name"
        label="姓名"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="mobile"
        label="手机号"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="identity_number"
        label="身份证号"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="role_name"
        label="角色"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="wechat_statu"
        label="绑定微信"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="status"
        label="状态"
      />
      <el-table-column
        align="center"
        show-overflow-tooltip
        prop="create_time"
        label="创建时间"
        width="170"
      />
      <el-table-column align="center" label="操作" width="140">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDisable(row)">
            {{ row.status === '启用' ? '禁用' : '启用' }}
          </el-button>
          <el-button
            type="text"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.page"
      :page-size="queryForm.list_row"
      :layout="layout"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { getList, doDelete, doDisable } from '@/api/manager'
  import Edit from './components/ManagerEdit'

  export default {
    name: 'Manager',
    components: { Edit },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          page: 1,
          list_row: 10,
          keyword: '',
          // status: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm('你确定要删除当前角色吗', null, async () => {
            const msg = await doDelete({ manager_id: row.id })
            this.$baseMessage(msg, 'success')
            this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.id).join()
            this.$baseConfirm('你确定要删除选中角色吗', null, async () => {
              const { msg } = await doDelete({ ids })
              this.$baseMessage(msg, 'success')
              this.fetchData()
            })
          } else {
            this.$baseMessage('未选中任何角色', 'error')
            return false
          }
        }
      },

      handleDisable(row) {
        const { id, status } = row
        const statusNew = status === '启用' ? '禁用' : '启用'
        this.$baseConfirm(`你确定要${statusNew}当前项吗`, null, async () => {
          const msg = await doDisable({ manager_id: id, status: statusNew })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.queryForm.list_row = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.page = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.page = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data, total } = await getList(this.queryForm)
        this.list = data
        if(this.list && this.list.length > 0){
          this.list.forEach(item=>{
            if(item.wechat_status === 2){
              item.wechat_statu = '未绑定'
            }else if(item.wechat_status === 1){
              item.wechat_statu = '已绑定：'+ item.wechat_info.nickname
            }
          })
        }
        console.log(222222,data)
        this.total = total
        this.listLoading = false
      },
    },
  }
</script>
